#include "CSGP_MessageSystem.h"

CSGP_MessageSystem* CSGP_MessageSystem::GetInstance(void)
{
	static CSGP_MessageSystem inst;
	return &inst;
}

void CSGP_MessageSystem::InitMessageSystem(MESSAGEPROC pfnMsgProc)
{
	if(!pfnMsgProc)
		return;

	m_pfnMsgProc = pfnMsgProc;
}

void CSGP_MessageSystem::SendMessageA(CBaseMessage* pMsg)
{
	// Protection check
	if(!pMsg)
		return;

	// Send message to queue
	m_MsgQueue.push(pMsg);
}

void CSGP_MessageSystem::ProcessMessages(void)
{
	// Check for message proc
	if(!m_pfnMsgProc)
		return;

	// Process messages in the queue
	while(!m_MsgQueue.empty())
	{
		m_pfnMsgProc(m_MsgQueue.front());
		delete m_MsgQueue.front();
		m_MsgQueue.pop();
	}
}

void CSGP_MessageSystem::ClearMessages(void)
{
	// Clear out messages
	while(!m_MsgQueue.empty())
	{
		delete m_MsgQueue.front();	// Delete message from front of queue
		m_MsgQueue.pop();
	}
}

void CSGP_MessageSystem::ShutdownMessageSystem(void)
{
	ClearMessages();

	m_pfnMsgProc = NULL;
}